﻿Imports System
Imports System.Threading

Namespace System.Workflow.Activities
    Friend NotInheritable Class CorrelationMethodResolver
        ' Methods
        Friend Sub New(ByVal interfaceType As Type)
            Me.interfaceType = interfaceType
        End Sub


        ' Properties
        Friend ReadOnly Property CorrelationProvider As ICorrelationProvider
            Get
                If (Me.correlationProvider Is Nothing) Then
                    SyncLock Me.corrProviderSync
                        If (Me.correlationProvider Is Nothing) Then
                            Dim provider As ICorrelationProvider = Nothing
                            Dim customAttributes As Object() = Me.interfaceType.GetCustomAttributes(GetType(CorrelationProviderAttribute), True)
                            If (customAttributes.Length = 0) Then
                                customAttributes = Me.interfaceType.GetCustomAttributes(GetType(ExternalDataExchangeAttribute), True)
                                Dim objArray2 As Object() = Me.interfaceType.GetCustomAttributes(GetType(CorrelationParameterAttribute), True)
                                If ((customAttributes.Length <> 0) AndAlso (objArray2.Length <> 0)) Then
                                    provider = New DefaultCorrelationProvider(Me.interfaceType)
                                Else
                                    provider = New NonCorrelatedProvider
                                End If
                            Else
                                Dim attribute As CorrelationProviderAttribute = [TryCast](customAttributes(0),CorrelationProviderAttribute)
                                provider = [TryCast](Activator.CreateInstance(attribute.CorrelationProviderType),ICorrelationProvider)
                            End If
                            Thread.MemoryBarrier
                            Me.correlationProvider = provider
                        End If
                    End SyncLock
                End If
                Return Me.correlationProvider
            End Get
        End Property


        ' Fields
        Private correlationProvider As ICorrelationProvider
        Private corrProviderSync As Object = New Object
        Private interfaceType As Type
    End Class
End Namespace

